package team.xiaobaiya.gptchatrobot.util;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 程小白
 */
public class Resp {
    private static final int DEFAULT_CAPACITY = 16;
    private static final int OK_CODE = 0;
    private static final int ERROR_DEFAULT_CODE = -1;
    private static final String ERROR_DEFAULT_MSG = "未知错误，请联系管理员";
    private static final String OK_MSG = "success";

    private Integer code;
    private String msg;
    private HashMap<String, Object> data;

    public Resp(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Resp ok() {
        return new Resp(OK_CODE, OK_MSG);
    }

    public static Resp error() {
        return new Resp(ERROR_DEFAULT_CODE, ERROR_DEFAULT_MSG);
    }

    public static Resp ok(String key, Object value) {
        Resp resp = new Resp(OK_CODE, OK_MSG);
        HashMap<String, Object> data = new HashMap<>(4);
        resp.setData(data);
        return resp;
    }
    public static Resp ok(String msg) {
        return new Resp(OK_CODE, msg);
    }

    public static Resp error(int code, String msg) {
        return new Resp(code, msg);
    }

    public static Resp error(ForCodeEnum codeEnum) {
        return new Resp(codeEnum.code(), codeEnum.msg());
    }
    public static Resp error(ForCodeEnum codeEnum, String extMsg) {
        return new Resp(codeEnum.code(), codeEnum.msg() + "," + extMsg);
    }

    public Resp putData(String key, Object data) {
        if (this.data == null){
            this.data = new HashMap<>(DEFAULT_CAPACITY);
        }
        this.data.put(key, data);
        return this;
    }

    public Resp putData(Map<String, Object> data) {
        if (this.data == null){
            this.data = new HashMap<>(DEFAULT_CAPACITY);
        }
        this.data.putAll(data);
        return this;
    }



    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(HashMap<String, Object> data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public HashMap<String, Object> getData() {
        return data;
    }

    public interface ForCodeEnum {
        /**
         * 获取状态码
         * @return 状态码
         */
        Integer code();

        /**
         * 获取错误信息
         * @return 错误信息
         */
        String msg();
    }



}
